package com.hishop.util;

import com.hishop.common.RedisPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

/**
 * Created by hejie on 2018/2/6.
 */
//@Slf4j
public class RedisPoolUtil {
    private static Logger log = LoggerFactory.getLogger(RedisPoolUtil.class);
    /**
     * 设置key的有效期，单位秒
     *
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, final int exTime) {
        Jedis jedis = null;
        Long ret = null;
        try {
            jedis = RedisPool.getJedis();
            ret = jedis.expire(key, exTime);
        } catch (Exception e) {
            log.error("set key:{} value:{} error:{}", key, exTime, e);
            RedisPool.returnBrokenResource(jedis);
            return ret;
        }
        RedisPool.returnResource(jedis);
        return ret;
    }

    /**
     * 增加key
     *
     * @param key
     * @param value
     * @return
     */
    public static String set(String key, String value) {
        Jedis jedis = null;
        String ret = null;
        try {
            jedis = RedisPool.getJedis();
            ret = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error:{}", key, value, e);
            RedisPool.returnBrokenResource(jedis);
            return ret;
        }
        RedisPool.returnResource(jedis);
        return ret;
    }

    /**
     * 增加key不重复
     *
     * @param key
     * @param value
     * @param exTime
     * @return
     */
    public static String setEx(String key, String value, int exTime) {
        Jedis jedis = null;
        String ret = null;
        try {
            jedis = RedisPool.getJedis();
            ret = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error:{}", key, value, e);
            RedisPool.returnBrokenResource(jedis);
            return ret;
        }
        RedisPool.returnResource(jedis);
        return ret;
    }

    /**
     * 根据key得到值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        Jedis jedis = null;
        String ret = null;
        try {
            jedis = RedisPool.getJedis();
            ret = jedis.get(key);
        } catch (Exception e) {
            log.error("set key:{} error:{}", key, e);
            RedisPool.returnBrokenResource(jedis);
            return ret;
        }
        RedisPool.returnResource(jedis);
        return ret;
    }

    /**
     * 删除key的值
     *
     * @param key
     * @return
     */
    public static Long del(String key) {
        Jedis jedis = null;
        Long ret = null;
        try {
            jedis = RedisPool.getJedis();
            ret = jedis.del(key);
        } catch (Exception e) {
            log.error("set key:{} error:{}", key, e);
            RedisPool.returnBrokenResource(jedis);
            return ret;
        }
        RedisPool.returnResource(jedis);
        return ret;
    }

    public static void main(String[] args) throws Exception {
        Jedis jedis = RedisPool.getJedis();
        RedisPoolUtil.set("testkey", "testvalue");
        String value = RedisPoolUtil.get("testkey");
        log.info("value:{}", value);
        RedisPoolUtil.setEx("a", "a", 3);
        Thread.sleep(1000);
        log.info("a:{}", RedisPoolUtil.get("a"));
        Thread.sleep(3000);
        log.info("a:{}", RedisPoolUtil.get("a"));
        RedisPoolUtil.set("b", "b");
        RedisPoolUtil.expire("b", 5);
        log.info("b:{}", RedisPoolUtil.get("b"));
        Thread.sleep(3000);
        RedisPoolUtil.del("b");
        log.info("b:{}", RedisPoolUtil.get("b"));
    }
}
